<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * This is a sample module for PyroCMS
 *
 * @author 		Jerel Unruh - PyroCMS Dev Team
 * @website		http://unruhdesigns.com
 * @package 	PyroCMS
 * @subpackage 	Sample Module
 */
class General_m extends MY_Model {

	public function __construct()
	{
		parent::__construct();
		$this->load->library('module_helper');
	}

	function add_sport_submit(){
		$sport_existed = $this->sport_entity->getArrayListOfSport();
		$canInsert = true;
		foreach($sport_existed as $object){
			if(strtolower($object->name) == strtolower($_POST['name'])){
				$canInsert = false;
				break;
			}
		}
		if($canInsert){
			$this->sport_entity->createNewSportEntity($_POST['name']);
			$this->session->set_flashdata('success', lang('dataman.success'));
			echo 'ok';
		}else{
			echo "Sport can not duplicate.";
		}
		exit();
	}
	
	function edit_sport_submit(){
		$SPORT = $this->sport_entity->init('id',intval($_POST['id']));
		
		$sport_existed = $this->sport_entity->getArrayListOfSport();
		$canInsert = true;
		foreach($sport_existed as $object){
			if( (strtolower($object->name) == strtolower($_POST['name'])) AND $object->id != $SPORT->getId() ){
				$canInsert = false;
				break;
			}
		}
		if($canInsert){
			$SPORT->setName($_POST['name']);
			$SPORT->update();
			$this->session->set_flashdata('success', lang('dataman.success'));
			echo 'ok';
		}else{
			echo "Sport can not duplicate.";
		}
		exit();
	}
	
	function add_team_submit(){
		$team_existed = $this->team_entity->getArrayListOfTeam();
		$canInsert = true;
		foreach($team_existed as $object){
			if( (strtolower($object->name) == strtolower($_POST['name'])) OR $object->feed_team_id == $_POST['feed_team_id']  )
			{
				$canInsert = false;
				break;
			}
		}
		if($canInsert){
			$team_id = $this->team_entity->createNewTeamEntity($_POST['name']);
			$TEAM = $this->team_entity->init('id', $team_id);
		//	$TEAM->setSportId($_POST['sport_id']);
			$flag = "";
			if(isset($_FILES["flag"]) AND !empty($_FILES["flag"]['name'])) {
				if(($_FILES["flag"]['size']/1000000) > allowMaxFileSize() ){
					$flag = '';
				}else{
					$flag = $this->module_helper->uploadFile( 
											$_FILES["flag"]['tmp_name'],
											$_FILES["flag"]['name'],
											$TEAM->getFolderFileStorage(),
											allowExtensionPictureUpload()
										);
				}	
			}
			$TEAM->setFeedTeamId($_POST['feed_team_id']);
			$TEAM->setCode($_POST['code']);
			$TEAM->setFlag($flag);
			$TEAM->update();
			$this->session->set_flashdata('success', lang('dataman.success'));
			echo 'ok';
		}else{
			echo "Team name or Feed Team ID can not duplicate.";
		}
		exit();
	}
	
	function edit_team_submit(){
		$TEAM = clone_($this->team_entity->init('id', $_POST['id']));
		$team_existed = $this->team_entity->getArrayListOfTeam();
		$canInsert = true;
		foreach($team_existed as $object){
			if( (strtolower($object->name) == strtolower($_POST['name'])) 
				 AND $object->id != $TEAM->getId() )
			{
				$canInsert = false;
				break;
			}
		}
		
		$TEAM2 = clone_($this->team_entity->init('feed_id', $_POST['feed_team_id']));
		if($TEAM2 AND $TEAM->getId() != $TEAM2->getId()){
			echo "Feed Team ID can not duplicate.";exit();
		}
		
		if($canInsert){
		//	$TEAM->setSportId($_POST['sport_id']);
			$flag = $TEAM->getFlag();
			if(isset($_FILES["flag"]) AND !empty($_FILES["flag"]['name'])) {
				if(($_FILES["flag"]['size']/1000000) > allowMaxFileSize() ){
					$flag = '';
				}else{
					$flag = $this->module_helper->uploadFile( 
											$_FILES["flag"]['tmp_name'],
											$_FILES["flag"]['name'],
											$TEAM->getFolderFileStorage(),
											allowExtensionPictureUpload()
										);
				}	
			}
			$TEAM->setFeedTeamId($_POST['feed_team_id']);	
			$TEAM->setCode($_POST['code']);
			$TEAM->setFlag($flag);
			$TEAM->setName($_POST['name']);
			$TEAM->update();
			$this->session->set_flashdata('success', lang('dataman.success'));
			echo 'ok';
		}else{
			echo "Team can not duplicate.";
		}
		exit();
	}
	
	function currency_submit(){
		if(!isset($_POST['id'])){
			if(empty($_POST['code'])){
				echo "Code must not empty.";exit();
			}
			if(empty($_POST['name'])){
				echo "Name must not empty.";exit();
			}
			if(! $this->phpvalidator->is_numeric($_POST['rate']) OR $_POST['rate'] <= 0){
				echo "Rate is not valid.";
				exit();
			}
			$EXISTED = $this->currency_entity->init('code', $_POST['code']);
			if($EXISTED){
				echo "Code is existed.";
				exit();
			}
			
			$newId = $this->currency_entity->createNewEntity();
			$CURRENCY = $this->currency_entity->init('id',$newId);
			$CURRENCY->setCode($_POST['code']);
			$CURRENCY->setName($_POST['name']);
			$CURRENCY->setRate($_POST['rate']);
			$CURRENCY->update();
			
			$flag = "";
			if(isset($_FILES["flag"]) AND !empty($_FILES["flag"]['name'])) {
				if(($_FILES["flag"]['size']/1000000) > allowMaxFileSize() ){
					$flag = '';
				}else{
					$flag = $this->module_helper->uploadFile( 
								$_FILES["flag"]['tmp_name'],
								$_FILES["flag"]['name'],
								$CURRENCY->getFolderFileStorage(),
								allowExtensionPictureUpload()
							);
				}	
			}
			$CURRENCY->setFlag($flag);
			$CURRENCY->update();
		}else{
			if(empty($_POST['code'])){
				echo "Code must not empty.";exit();
			}
			if(empty($_POST['name'])){
				echo "Name must not empty.";exit();
			}
			if(! $this->phpvalidator->is_numeric($_POST['rate']) OR $_POST['rate'] <= 0){
				echo "Rate is not valid.";
				exit();
			}
			$EXISTED = $this->currency_entity->init('code', $_POST['code']);
			if($EXISTED AND $EXISTED->getId() != $_POST['id']){
				echo "Code is existed.";
				exit();
			}
			$CURRENCY = $this->currency_entity->init('id',$_POST['id']);
			$CURRENCY->setCode($_POST['code']);
			$CURRENCY->setName($_POST['name']);
			$CURRENCY->setRate($_POST['rate']);
			$CURRENCY->update();
			
			$flag = $CURRENCY->getFlag();
			if(isset($_FILES["flag"]) AND !empty($_FILES["flag"]['name'])) {
				if(($_FILES["flag"]['size']/1000000) > allowMaxFileSize() ){
					$flag = '';
				}else{
					$flag = $this->module_helper->uploadFile( 
											$_FILES["flag"]['tmp_name'],
											$_FILES["flag"]['name'],
											$CURRENCY->getFolderFileStorage(),
											allowExtensionPictureUpload()
										);
				}	
			}
			$CURRENCY->setFlag($flag);
			$CURRENCY->update();
		}
		$this->session->set_flashdata('success', lang('dataman.success'));
		echo 'ok';exit();
	}
	
	function currency_delete(){
		$this->db->where('id', $_POST['id'])->delete(TBL_CURRENCY);
		$this->session->set_flashdata('success', lang('dataman.success'));
		exit();
	}
	
	function add_coin_packages_submit(){
		if(!isset($_POST['id'])){
			if(empty($_POST['package'])){
				echo "Package must not empty.";exit();
			}
			if(! $this->phpvalidator->is_natural($_POST['coin']) OR $_POST['coin'] <= 0){
				echo "Coin is not valid.";
				exit();
			}
			if(! $this->phpvalidator->is_numeric($_POST['cost']) OR $_POST['cost'] <= 0){
				echo "Cost is not valid.";
				exit();
			}
			$newId = $this->coin_packages_entity->createNewEntity();
			$COIN_PACKAGE = $this->coin_packages_entity->init('id',$newId);
			$COIN_PACKAGE->setPackage($_POST['package']);
			$COIN_PACKAGE->setCoin($_POST['coin']);
			$COIN_PACKAGE->setCost($_POST['cost']);
			$COIN_PACKAGE->setType($_POST['type']);
			$COIN_PACKAGE->update();
			
		}else{
			if(empty($_POST['package'])){
				echo "Package must not empty.";exit();
			}
			if(! $this->phpvalidator->is_natural($_POST['coin']) OR $_POST['coin'] <= 0){
				echo "Coin is not valid.";
				exit();
			}
			if(! $this->phpvalidator->is_numeric($_POST['cost']) OR $_POST['cost'] <= 0){
				echo "Cost is not valid.";
				exit();
			}
			$COIN_PACKAGE = $this->coin_packages_entity->init('id',$_POST['id']);
			$COIN_PACKAGE->setPackage($_POST['package']);
			$COIN_PACKAGE->setCoin($_POST['coin']);
			$COIN_PACKAGE->setCost($_POST['cost']);
			$COIN_PACKAGE->setType($_POST['type']);
			$COIN_PACKAGE->update();
			
		}
		$this->session->set_flashdata('success', lang('dataman.success'));
		echo 'ok';exit();
	}
	
	function coin_package_delete(){
		$this->db->where('id', $_POST['id'])->delete(TBL_COIN_PACKAGES);
		$this->session->set_flashdata('success', lang('dataman.success'));
		exit();
	}
	
	function add_edit_category_submit(){
		if(!isset($_POST['id'])){
			if(empty($_POST['name'])){
				echo "Category name must not empty.";exit();
			}
			$newId = $this->pre_define_entity->createNewEntity();
			$PRE_DEFINE = $this->pre_define_entity->init('id',$newId);
			$PRE_DEFINE->setParentId(NULL);
			$PRE_DEFINE->setIsCat('yes');
			$PRE_DEFINE->setIsEvent(NULL);
			$PRE_DEFINE->setTitle($_POST['name']);
			$PRE_DEFINE->setOdds(NULL);
			$PRE_DEFINE->setSportId($_POST['sport_id']);
			$PRE_DEFINE->update();
		}else{
			if(empty($_POST['name'])){
				echo "Category name must not empty.";exit();
			}
			$PRE_DEFINE = $this->pre_define_entity->init('id',$_POST['id']);
			$PRE_DEFINE->setTitle($_POST['name']);
			$PRE_DEFINE->setSportId($_POST['sport_id']);
			$PRE_DEFINE->update();
		}
		
		$this->session->set_flashdata('success', lang('dataman.success'));
		echo 'ok';exit();
	}
	
	function delete_default_category(){
		$id = $_POST['id'];
		$event_array = $this->pre_define_entity->getListEventOfCategory($id);
		foreach($event_array as $ev){
			$this->db->where('id',$ev->id)->delete(TBL_PRE_DEFINE);
		}	
		$this->db->where('id',$id)->delete(TBL_PRE_DEFINE);
		$this->session->set_flashdata('success', lang('dataman.success'));
		echo 'ok';exit();
	}
	
	function add_edit_event_submit(){
		if(!isset($_POST['id'])){
			if(empty($_POST['title'])){
				echo "Event name must not empty.";exit();
			}
			if(! $this->phpvalidator->is_numeric($_POST['odds']) OR $_POST['odds'] <= 0){
				echo "Odds is not valid.";
				exit();
			}
			$newId = $this->pre_define_entity->createNewEntity();
			$PRE_DEFINE = $this->pre_define_entity->init('id',$newId);
			$PRE_DEFINE->setParentId($_POST['cat_id']);
			$PRE_DEFINE->setIsCat(NULL);
			$PRE_DEFINE->setIsEvent('yes');
			$PRE_DEFINE->setTitle($_POST['title']);
			$PRE_DEFINE->setOdds($_POST['odds']);
			$PRE_DEFINE->update();
		}else{
			$PRE_DEFINE = $this->pre_define_entity->init('id',$_POST['id']);
			$PRE_DEFINE->setParentId($_POST['cat_id']);
			$PRE_DEFINE->setTitle($_POST['title']);
			$PRE_DEFINE->setOdds($_POST['odds']);
			$PRE_DEFINE->update();
		}
		echo 'ok';exit();
	}
	
	function delete_default_event(){
		$id = $_POST['id'];
		$this->db->where('id',$id)->delete(TBL_PRE_DEFINE);
		$this->session->set_flashdata('success', lang('dataman.success'));
		echo 'ok';exit();
	}
	
	function auction_coin_submit(){
		extract($this->input->post());
		 
		if(!strlen($from)){
			echo 'From credits must not empty';
			exit();
		}
		if(!strlen($to)){
			echo 'To credits must not empty';
			exit();
		}
		if(!strlen($coins)){
			echo 'Coins must not empty';
			exit();
		}
		if(!strlen($days_accept)){
			echo 'Days Accept must not empty';
			exit();
		}
		
		if(!($id)){
			$newid = $this->auction_coupon->createNewEntity();
			$PACK = $this->auction_coupon->init('id',$newid);
			$PACK->setFrom($from);
			$PACK->setTo($to);
			$PACK->setCoins($coins);
			$PACK->setDaysAccept($days_accept);
			$PACK->update();
		}else{
			$PACK = $this->auction_coupon->init('id',$id);
			$PACK->setFrom($from);
			$PACK->setTo($to);
			$PACK->setCoins($coins);
			$PACK->setDaysAccept($days_accept);
			$PACK->update();
		}
		$this->session->set_flashdata('success', lang('dataman.success'));
		echo 'ok';exit();
	}
	
	function auction_coin_delete(){
		extract($this->input->post());
		$this->db->where('id',$id)->delete(TBL_AUCTION);
		$this->session->set_flashdata('success', lang('dataman.success'));
		echo 'ok';exit();
	}
	
	function setting_submit(){
		extract($this->input->post());
		$maxCoins = intval($max_coins);
		
		$rs = $this->db->where('id',1)->get('default_cpg_status')->result();
		if(!empty($rs)){
			$this->db->where('id',1)->update('default_cpg_status',array('name'=>$maxCoins));
		}else{
			$this->db->insert('default_cpg_status',array('name'=>$maxCoins,'id'=>1));
		}
		
		$this->session->set_flashdata('success', lang('dataman.success'));
		redirect(site_url("admin/dataman/general/setting"));
	}
	
	function reset_coins(){
		$this->db->update('default_cpg_user',array('coin'=>0));
		$this->session->set_flashdata('success', lang('dataman.success'));
		redirect(site_url("admin/dataman/general/setting"));
	}
	
	
	
	
	//end class
}
